/*
@author: tk
@file: 二叉树.go
@time: 2022/12/3 17:55
*/
package main

import "fmt"

// 定义节点属性
type Node struct {
	item  int
	left *Node
	right *Node
}

// 二叉树
type Tree struct {
	root *Node
}

// 生成二叉树
func (tree *Tree) createTree(value int) {
	node := &Node{item: value}
	fmt.Println("增加节点", value)
	if tree.root == nil {
		tree.root = node
		return
	}else {
		var queue []*Node = []*Node{tree.root}
		for len(queue) != 0 {
			cur := queue[0]
			queue = queue[1:]
			if cur.left == nil {
				cur.left = node
				return
			}else if cur.right == nil {
				cur.right = node
				return
			}else {
				queue = append(queue, cur.left)
				queue = append(queue, cur.right)
			}
		}
	}
}

func main() {
	var tree Tree
	tree.createTree(2)
	tree.createTree(10)
	tree.createTree(3)
	tree.createTree(5)
}

